package cn.edu.jgsu.jgsuchat.controller;

import cn.edu.jgsu.jgsuchat.mapper.UserDao;
import cn.edu.jgsu.jgsuchat.pojo.customPojo.SelectFriendsByUserId;
import cn.edu.jgsu.jgsuchat.pojo.databasePojo.Friends;
import cn.edu.jgsu.jgsuchat.service.RedisDataService;
import cn.edu.jgsu.jgsuchat.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 作者: xyn
 * 创建时间: 2020/3/25 上午4:24x-special/nautilus-clipboard
 * copy
 * smb://xiaoxin/desktop/17%E8%BD%AF%E4%BB%B6%E5%B7%A6%E5%86%9B-%E6%97%B6%E5%85%89%E6%9C%BA.pptx
 */
@RestController
@RequestMapping("RedisChatController")
public class RedisChatController {
    @Autowired
    RedisDataService redisDataService;

    @RequestMapping("getChatData")
    public Map retrunChatData(String getWhoseChatData, String getWhoSend) {
        Map map = redisDataService.getMessage(getWhoseChatData, getWhoSend);
        long i = (long) map.get("counts");
        if (i == 0) {
            map = new HashMap();
            map.put("message", "0");
            return map;
        } else {

            return map;

        }


    }

    @RequestMapping("loginInit")
    public String init() {
        redisDataService.testSample();

        return "初始数据装填完成";
    }
/*    @RequestMapping("friendsList")
    public Map friendsList(int id) {
        List<SelectFriendsByUserId> friendslist = redisDataService.getFriendsById(id);
        Map map = new HashMap();
        Map map2 = new HashMap();
        LinkedList list = new LinkedList();
        for (SelectFriendsByUserId selectFriendsByUserId : friendslist) {
            map2.put(selectFriendsByUserId.getFriendId(),selectFriendsByUserId.getFriendName());
        }

;
        map.put("name",map2);
        System.out.println("发送成功");
        return map;
    }*/
    @RequestMapping("sendMessage")
    public JsonData sendMessage(String whoSend,String sendToWho, String sendWhat) {
        redisDataService.storeChatData(sendToWho, whoSend, sendWhat);

        return  new JsonData(1,null,"成功发送消息");
    }
    /**
     * 编写轮询接口,没新消息就告诉没有,
     * 客户端要编写一个常驻通知: 类似于  无新消息,上次更新 1秒前
     * 如果有新消息返回变动了的
     * 有则通知
     * @param getWhoseChatData
     * @return
     */
@RequestMapping("hasNewMessageAndSendBack")
    public JsonData hasNewMessageAndSendBack(String getWhoseChatData){
        //list存放变动了的
        List changeList = redisDataService.hasNewMessage(getWhoseChatData);

        //一个map存储一个人发来的消息
        Map map = null;
        //一个List存储所有getWhoseChatData的消息
        List<Map> allMessage = new LinkedList();
        if(changeList!=null) {
            System.out.println(changeList);

            for (Object o : changeList) {
              map = redisDataService.getMessage(getWhoseChatData,String.valueOf(o));
              allMessage.add(map);
            }
            return new JsonData(1,allMessage,"轮询:您有新的消息");
        }
        else {
            return new JsonData(0,null,"轮询:无新消息");
        }

    }


}
